package com.dependencies.thymeleaf.conroller;

import com.dependencies.thymeleaf.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;

/**
 * @author zhizhou   2024/7/26 22:33
 */
@Controller
public class HelloController {
    
    @GetMapping("/hello")
    public String index(Model model) {
        model.addAttribute("name", "Thymeleaf User");
        model.addAttribute("home", "http://www.thymeleaf.org");
    
        //<!--#### `th:unless`-->
        //<!--用于条件渲染（取反）。-->
        model.addAttribute("userinfo", null);
    
        //<!--### 3. 条件判断-->
        //<!--#### `th:if`-->
        //<!--用于条件渲染。-->
        User user = new User();
        user.setName("zhizhou");
        model.addAttribute("user", user);
        
        //<!--### 4. 循环迭代-->
        //<!--`th:each`-->
        //<!--用于迭代集合。-->
        model.addAttribute("users", Arrays.asList("zhizhou", "zhangsan", "lisi"));
        //<!-- 索引和状态变量-->
        model.addAttribute("userList", Arrays.asList(user, user, user));
        return "hello";
    }
}
